package com.zdkj.subway.rca.controller;

import com.zdkj.common.core.util.R;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.rca.dto.CkMarketingTicketElecConfirmDTO;
import com.zdkj.subway.rca.entity.SwCkMarketingTicketElec;
import com.zdkj.subway.rca.enums.SW_CK_ELECTRONIC_RECEIPT_RETURN_ORDER_TYPE;
import org.springframework.data.redis.core.RedisTemplate;

import java.time.LocalDateTime;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author cooze
 * @version 1.0.0 创建于 2020/10/24
 * @deprecated 电子回执单管理
 **/
public abstract class SwCkElectronicReceiptBaseController extends BaseController {

    /**
     * 获取幂等Key
     *
     * @param uid
     * @param type
     * @param uniqueKey
     * @return
     */
    protected String getIdeKey(int uid, String type, String uniqueKey) {
        return String.format("subway:swck:elec:%d:%s:%s", uid, type, uniqueKey);
    }
    /**
     * 数据验证
     * @param returnOrderType
     * @return
     */
    protected Optional<R> validate(String returnOrderType) {
        if (SW_CK_ELECTRONIC_RECEIPT_RETURN_ORDER_TYPE.isInValid(returnOrderType)) {
            return Optional.of(R.failed("电子回执单类型必填"));
        }
        return Optional.empty();
    }

   /**
    * 验证是否重复提交
    * @param uid
    * @param type
    * @param uniqueKey
    * @return
    */
    protected boolean noPass(int uid, String type, String uniqueKey) {
        String ideKey = getIdeKey(uid, type, uniqueKey);
        if (redisTemplate().hasKey(ideKey)) {
            return true;
        }
        //不能重复提交
        redisTemplate().opsForValue().set(ideKey, "1", 10, TimeUnit.SECONDS);
        return false;
    }


    public abstract RedisTemplate<String, String> redisTemplate();

    /**
     * 查找对象信息
     * @param dto
     */
    protected void fixCkMarketingTicketElecConfirmData(CkMarketingTicketElecConfirmDTO dto) {
        PigxUser pigxUser = SecurityUtils.getUser();
        for (SwCkMarketingTicketElec elec : dto.getMtEleList()) {
            elec.setCreatorId(pigxUser.getId());
            elec.setCreatorName(pigxUser.getNikeName());
            elec.setConfirmStatus("1");
            elec.setCreateTime(LocalDateTime.now());
        }

    }
}
